//引入express模块
const express = require("express");
//引入数据库连接模块
const pool = require("../pool.js");
//引入公用函数
const xsp_fn = require("../others/xsp_fn");
//创建路由器对象
const mR = express.Router();
//以下为接口部分

//注册接口
//1.1接口地址：http://127.0.0.1:3000/user/add
mR.post("/add", (req, res, next) => {
    let ss=req.body
    let ff=/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/

    if(ss.user_name==""){
       return res.send({msg:"用户名不能为空"})
    }
    if(ss.account_number==""){
        return res.send({msg:"账户不能为空"})
    }
    if(ss.user_phone==""){
        return res.send({msg:"手机号不能为空"})

    }else if(!ff.test(user_phone)){
        return res.send({msg:"手机号格式不正确"})

    }
    if(ss.password==""){
        return res.send({msg:"密码不能为空"})

    }
  let c_add = xsp_fn.api_add("user_info");
  pool.query(c_add.sql, [req.body], (err, data) => {
    if (err) {
      res.send(c_add.err);
      next(err);
    } else {
      console.log(data);
      res.send(c_add.success);
    }
  });
});

//1.2接口地址：http://127.0.0.1:3000/user/login
mR.get("/login", (req, res, next) => {
  // console.log(req.query.account_number);
  let zz = req.query.account_number;
  let ss = req.query.password;
  if (zz == "") {
    return res.send({ msg: "账户不能为空" });
  }
  if (ss == "") {
    return res.send({ msg: "密码不能为空" });
  }
  pool.query(
    "select * from user_info where account_number=? and password=?",
    [req.query.account_number, req.query.password],
    (err, data) => {
      if (err) {
        next(err);
        return;
      }

      if (data[0]) {
        res.send({
          code: 1,
          msg: "登录成功",
          data,
        });
      } else {
        res.send({ code: 0, msg: "登录失败" });
      }
    }
  );
});

//查看用户信息
//接口地址：http://127.0.0.1:3000/user/userinfo
//接口方式：get
mR.get("/userinfo", (req, res, next) => {
  // console.log(req.query.uid);
  pool.query(
    "select * from user_info where user_id=?",
    [req.query.user_id],
    (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      res.send({ code: 1, msg: "查询成功", data });
    }
  );
});


//忘记密码：http://127.0.0.1:3000/user/wang
//接口方式：get
//
mR.get("/wang", (req, res, next) => {
    // console.log(req.query.uid);
    pool.query(
      "select * from user_info where user_phone=?",
      [req.query.user_phone],
      (err, data) => {
        if (err) {
          console.log(err);
          next(err);
          return;
        }
        let cc=data[0].user_id
        res.send({ code: 1, msg: "查询成功", data:cc});
      }
    );
  });

//用户修改接口
//3.1接口地址：http://127.0.0.1:3000/user/update
mR.put("/update", (req, res, next) => {
  console.log(req.body.user_id);
    let c_update = `update user_info set ? where user_id=?`;
    pool.query(c_update, [req.body, req.body.user_id], (err, data) => {
        if (err) {
            next(err);
            return;
        }
        if (data.changedRows) {
          res.send({ code: 1, msg: "修改成功" });

          } else {
            res.send({ code: 0, msg: "修改失败" });

          }
    });
});

//删除用户接口
//接口地址：http://127.0.0.1:3000/user/delete
//接口方式：delete
mR.delete("/delete", (req, res, next) => {
    let c_delete = `delete from user_info where user_id=?`;
    pool.query(c_delete, [req.query.user_id], (err, data) => {
      if (err) {
        next(err);
        return;
      }
      if (data.changedRows) {
        res.send({ code: 0, msg: "删除失败" });
      } else {
        res.send({ code: 1, msg: "删除成功" });
      }
    });
  });


//暴露出去
module.exports = mR;
